package arpui.util;

import java.util.ArrayList;
import java.util.List;

import jpcap.JpcapCaptor;
import jpcap.JpcapSender;
import jpcap.NetworkInterface;
import jpcap.packet.Packet;

public class ARPFindUtil {
	public static List getAllAdapter() throws Exception{
		List list = null;
		
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();
		if(devices == null){
			throw new Exception("无可用网卡!");
		}
		list = new ArrayList();
		int len = devices.length;
		for(int i=0;i<len;i++){
			list.add(devices[i]);
		}
		return list;
	}
	
	public static NetworkInterface getNetCardByID(int index){
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();
		return devices[index];
	}
	
//	public static void findDevice(int cardIndex){
//		new Thread(new StartFindRun(cardIndex)).start();
//	}
//	
//	public static void findDevice(int cardIndex,int time){
//		new Thread(new StartFindRun(cardIndex,time)).start();
//	}
	
	//将byte[]转为 hexString
	public static String bytesToHexStr(byte[] b){
		StringBuffer str = new StringBuffer();
		for(int i=0;i<b.length;i++){
			int v = b[i] & 0xFF;
			String hv = Integer.toHexString(v); 
			if(hv.length()<2){
				hv = "0"+hv;
			}
//			System.out.print(hv +" ");
			if(i!=b.length-1){
				str.append(hv+" ");
			}else{
				str.append(hv);
			}
			if((i+1)%8==0){
				str.append("\n");
			}
		}
		return str.toString().toUpperCase();
	}
	
	public static Long macBytesToLong(byte[] tmp){
		Long retV = 0L;
		
		retV=(((long)0&0xff)<<56) 
                 |(((long)0&0xff)<<48) 
                 |(((long)tmp[0]&0xff)<<40) 
                 |(((long)tmp[1]&0xff)<<32) 
                 |(((long)tmp[2]&0xff)<<24) 
                 |(((long)tmp[3]&0xff)<<16) 
                 |(((long)tmp[4]&0xff)<<8) 
                 |(((long)tmp[5]&0xff)<<0); 
		
		return retV;
	}
	
	/**
	 * long转化为 数组mac
	 * @param mac
	 * @return
	 */
	public static byte[] longToBytes(Long mac){
		byte[] retV = new byte[6];
		
		byte[] bytes = new byte[8];
		bytes[0]=(byte)(mac>>56); 
		bytes[1]=(byte)(mac>>48); 
		bytes[2]=(byte)(mac>>40); 
		bytes[3]=(byte)(mac>>32); 
		bytes[4]=(byte)(mac>>24); 
		bytes[5]=(byte)(mac>>16); 
		bytes[6]=(byte)(mac>>8); 
		bytes[7]=(byte)(mac>>0); 
		
		System.arraycopy(bytes, 2, retV, 0 , 6);
		return retV;
	}
	
	
	public static String bytesToIP(byte[] b){
		StringBuffer ip = new StringBuffer();;
		if(b.length != 4){
			return null;
		}
		for(int i=0;i<4;i++){
			int v = b[i] & 0xFF;
			if(i!=3){
				ip.append(v+".");
			}else{
				ip.append(v);
			}
		}
//		System.out.println(ip.toString());
		return ip.toString();
	}
	
	public static String bytesToString(byte[] b){
		return new String(b);
	}
	
	public static char bytesToChar(byte[] b){
		char res = 0;
		res = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
		return res;
	}
	
	public static short bytesToShort(byte[] b){
		short res = 0;
		return (short) (((b[0] << 8) | b[1] & 0xff));  
	}
	
	public static int bytesToInt(byte[] buf) {
		int firstByte = 0;
		int secondByte = 0;
		int thirdByte = 0;
		int fourthByte = 0;
		int index = 0;
		firstByte = (0x000000FF & ((int) buf[index]));
		secondByte = (0x000000FF & ((int) buf[index + 1]));
		thirdByte = (0x000000FF & ((int) buf[index + 2]));
		fourthByte = (0x000000FF & ((int) buf[index + 3]));
		index = index + 4;
		return (int) (((int) (firstByte << 24 | secondByte << 16
				| thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL);
	}
	
	//32->0,0,0,32
	public static byte[] intToBytes(int i){
		byte[] a = new byte[4];
        a[3] = (byte) (0xff & i);
        a[2] = (byte) ((0xff00 & i) >> 8);
        a[1] = (byte) ((0xff0000 & i) >> 16);
        a[0] = (byte) ((0xff000000 & i) >> 24);
		return a;
	}
	
	public static byte[] shortToBytes(short s){
		byte[] targets = new byte[2];
		for (int i = 0; i < 2; i++) {
			int offset = (targets.length - 1 - i) * 8;
			targets[i] = (byte) ((s >>> offset) & 0xff);
		}
		return targets;
	}
	
	public static JpcapCaptor sendArp(byte[] b,NetworkInterface device) throws Exception{
		if(device ==  null){
			return null;
		}
		JpcapCaptor captor = JpcapCaptor.openDevice(device, 2000, false, 3000);
		captor.setFilter("arp", false);
//		JpcapSender jsend = JpcapSender.openDevice(device);
		JpcapSender jsend = captor.getJpcapSenderInstance();
		
		Packet p = new Packet();
		p.data = b;
		
		jsend.sendPacket(p);
		
		return captor;
	}
	
	
	public static JpcapCaptor sendArp(byte[] b,JpcapCaptor captor) throws Exception{
		
		JpcapSender jsend = captor.getJpcapSenderInstance();
		
		Packet p = new Packet();
		p.data = b;
		
		jsend.sendPacket(p);
		
		return captor;
	}
}
